$(document).ready(function(){
	//create a new div
	var newDiv = document.createElement("div");
	var newDivChild = document.createElement("div"); 
	newDiv.id = "DOMdialog";
	newDivChild.className = "content";
	newDiv.appendChild(newDivChild)
	document.body.insertBefore(newDiv, document.body.childNodes[0]);

	//get the div
	var dialogDiv = $('div#DOMdialog');

	$('a.printclasslistone').on('click',function(e){
		e.preventDefault();
		
		$( "#msgDialog" ).on( "dialogbeforeclose", function( event, ui ) {
            $("#msgDialog > div.content").empty();
        } );

		var url = $(this).attr('href');
		var url_to = $(this).attr('data-url-to');
		$.ajax({
			type:'get',
			url:url,
			data:''
		}).done(function(e){
			console.log(e);

			if(e.stat == true)
			{
				var thehtml = '';
				$.each(e.data,function(key,value){
					thehtml += "<a href='"+url_to+"/"+key+"' class='btn btn-small' target='_blank' style='margin:2px;'><i class='icon-file'></i> "+value+"</a><br>";
				});

				$("#msgDialog").dialog('option','width',400);
				$("#msgDialog").dialog('option','title','Select section to print.');
				$("#msgDialog").dialog('option', 'buttons', {
						"Close" : function() {
						$(this).dialog("close");
				}
				});
				$("#msgDialog > div.content").html(thehtml);
				$("#msgDialog").dialog("open");

			}else{

			}
		});
	});

	$('a#print_birthday_list').on('click',function(e){
		e.preventDefault();

		var form = '';
		var href = $(this).attr('href');
		
		$(dialogDiv).dialog({
			autoOpen: false,
			show: "blind",
			hide: "explode",
			resizable : false,
			draggable: false,
			minHeight: '85%',
			minWidth: '580',
			modal:true,
			open: function( event, ui ) {
				$(document).find('body').css('overflow','hidden');
			},
			close:function(){
				$(document).find('body').css('overflow','scroll');	
			},
			title:'Enter Dates to be generated.'
		});

		//form
		form += '<form id="dynamic_birthday_form">';
		form += '<div><label class="label">Date Column 1</label class="label"><input class="datepick text ui-widget-content ui-corner-all" name="datecolumn[]" type="text" placeholder="MM/DD/YYYY"></div>';
		//form += '<div><label class="label">Date Column 2</label class="label"><input class="datepick text ui-widget-content ui-corner-all"name="datecolumn[]" type="text" placeholder="MM/DD/YYYY"></div>';
		//form += '<div><label class="label">Date Column 3</label class="label"><input class="datepick text ui-widget-content ui-corner-all"name="datecolumn[]" type="text" placeholder="MM/DD/YYYY"></div>';
		//form += '<div><label class="label">Date Column 4</label class="label"><input class="datepick text ui-widget-content ui-corner-all"name="datecolumn[]" type="text" placeholder="MM/DD/YYYY"></div>';
		//form += '<div><label class="label">Date Column 5</label class="label"><input class="datepick text ui-widget-content ui-corner-all"name="datecolumn[]" type="text" placeholder="MM/DD/YYYY"></div>';
		form += '</form">';
		//endform
		
		dialogDiv.find('div.content').html(form);
		$(".datepick").datepicker({
			changeMonth: true,
            changeYear: true
       	});
		
		var  dynamicForm =  $('form#dynamic_birthday_form');

		dialogDiv.dialog('option', 'buttons', {
            "Generate Report" : function() {
            	var check =$(dynamicForm).serializeArray();
            	var param = $.param(check);
				var url = href+"?"+param;
            	window.open(url);
            },
            "Generate And Close" : function() {
            	var check =$(dynamicForm).serializeArray();
            	var param = $.param(check);
				var url = href+"?"+param;
            	window.open(url);
            	$(this).dialog("close");
            },
            "Close" : function() { $(this).dialog("close"); }
   	 	});

		
		dialogDiv.dialog("open");
	});

	

});